local sijie = fk.CreateSkill{
  name = "dajubei__sijie"
}

Fk:loadTranslationTable{
  ["dajubei__sijie"] = "死节",
  [":dajubei__sijie"] = "濒死状态，你可以场上的坐骑牌当【桃】使用，且其他角色的【桃】对你失效。",
  ["#dajubei__sijie"] = "死节：你可以将场上坐骑牌当【桃】使用。",

  ["$dajubei__sijie1"] = "今吾明主，竟杜绝言路。",
  ["$dajubei__sijie2"] = "昔京兆尹，敢面引庭争。"
}

local TuntianUtil = require "packages/tuntian_studio/utility"

--- 找场上的所有马
---@param room AbstractRoom
---@return integer[]
local getHorses = function (room)
  local horses = {}
  for _, p in ipairs(room.alive_players) do
    for _, sub in ipairs{Card.SubtypeOffensiveRide, Card.SubtypeDefensiveRide} do
      local horse = p:getEquipments(sub)
      if #horse > 0 then
        for _, id in ipairs(horse) do
          table.insert(horses, id)
        end
      end
    end
  end
  return horses
end

sijie:addEffect("viewas", {
  anim_type = "support",
  prompt = "#dajubei__sijie",
  pattern = "peach",
  interaction = function (self, player)
    local horses = getHorses(Fk:currentRoom())
    local details = table.map(horses, function (id) return TuntianUtil.getCardData(id, Fk:currentRoom()) end)
    return TuntianUtil.CardNameBox { choices = horses, all_choices = details }
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    if not self.interaction.data then return end
    local peach = Fk:cloneCard("peach")
    peach.skillName = sijie.name
    peach:addSubcard(self.interaction.data)
    return peach
  end,
  enabled_at_play = function (self, player)
    return false
  end,
  enabled_at_response = function (self, player, response)
    return not response and player.dying and #getHorses(Fk:currentRoom()) > 0
  end
})

sijie:addEffect(fk.PreCardEffect, {
  anim_type = "negative",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(sijie.name) and target and player ~= target then
      return data.card.trueName == "peach" and data.to == player
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    data.use.nullifiedTargets = data.use.nullifiedTargets or {}
    table.insert(data.use.nullifiedTargets, player)
  end
})

return sijie